import 'dart:io';
import 'dart:isolate';

void main() {
  print("main isolate start");
  multiThread();
  print("main isolate stop");
}



void multiThread(){
  print("multiThread isolate start");
  //构建一个接受端口
  ReceivePort r1 = ReceivePort();
  SendPort sp1 = r1.sendPort;

  Isolate.spawn(newThread, sp1);


  r1.listen((message) {
    print("main接受到消息：$message");
  });

  print("multiThread isolate end");
}

void newThread(SendPort p1){

  ReceivePort receivePort = ReceivePort();
  receivePort.listen((message) {
    print("child接收到消息：$message");
  });

  // 模拟耗时5秒
  sleep(Duration(seconds:5));

  String msg = "这是新Isolate发送的消息！！！";
  p1.send(msg);
}